package com.hex.ds.hdtp.core.inf.script.analyze.repository.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.common.page.converter.IPageAndMyBatisPlusPageConverter;
import com.hex.ds.hdtp.core.inf.script.analyze.mapper.IScriptAnalyzeListMapper;
import com.hex.ds.hdtp.core.inf.script.analyze.po.ScriptAnalyzeListPo;
import com.hex.ds.hdtp.core.inf.script.analyze.repository.IScriptAnalyzeListRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package com.hex.ds.hdtp.core.inf.script.analyze.repository.impl
 * @ClassName ScriptAnalyzeListRepository
 * @Description 脚本分析清单-数据服务接口实现类
 * @Author gj.xu
 * @Date 2023/12/8 14:24
 * @Version v2.0
 **/
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptAnalyzeListRepository implements IScriptAnalyzeListRepository {

    private final IScriptAnalyzeListMapper scriptAnalyzeListMapper;

    private final IPageAndMyBatisPlusPageConverter pageConverter;

    /**
     * @Method queryListByPage
     * @Param
     * @Param scriptAnalyzeListPo
     * @Param page
     * @Return com.hex.ds.hdtp.core.inf.common.page.Page<com.hex.ds.hdtp.core.inf.script.analyze.po.ScriptAnalyzeListPo>
     * @Description 分页查询
     * @Author gj.xu
     * @Date 2023/12/8 14:27
     * @Version V2.0
     */
    @Override
    public Page<ScriptAnalyzeListPo> queryListByPage(ScriptAnalyzeListPo scriptAnalyzeListPo, Page page) {
        final IPage tmpPageResult = scriptAnalyzeListMapper.queryListByPage(
                pageConverter.toMyBatisPlusPage(page)
                , scriptAnalyzeListPo
        );
        final List list = tmpPageResult.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageResult);
        return pageResult.setData(list);
    }

    @Override
    public Page<ScriptAnalyzeListPo> queryConsanguinityListByPage(ScriptAnalyzeListPo scriptAnalyzeListPo, Page toPage) {
        final IPage tmpPageResult = scriptAnalyzeListMapper.queryConsanguinityListByPage(
                pageConverter.toMyBatisPlusPage(toPage)
                , scriptAnalyzeListPo
        );
        final List list = tmpPageResult.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageResult);
        return pageResult.setData(list);
    }

    /**
     * @Method queryListByTaskId
     * @Param
     * @Param analyzeTaskPkId
     * @Param status
     * @Return java.util.List<com.hex.ds.hdtp.core.inf.script.analyze.po.ScriptAnalyzeListPo>
     * @Description 根据指定的分析任务主键查询清单
     * @Author gj.xu
     * @Date 2023/12/8 15:39
     * @Version V2.0
     */
    @Override
    public List<ScriptAnalyzeListPo> queryListByTaskId(String analyzeTaskPkId, String status) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("analyze_task_pk_id", analyzeTaskPkId);
        if (StrUtil.isNotBlank(status)){
            queryWrapper.eq("status", status);
        }
        return scriptAnalyzeListMapper.selectList(queryWrapper);
    }

    /**
     * @Method removeByTaskId
     * @Param
     * @Param convertTaskPkId
     * @Return java.lang.Integer
     * @Description 根据指定的分析任务删除数据
     * @Author gj.xu
     * @Date 2023/12/8 15:39
     * @Version V2.0
     */
    @Override
    public Integer removeByTaskId(String convertTaskPkId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("analyze_task_pk_id", convertTaskPkId);
        return scriptAnalyzeListMapper.delete(queryWrapper);
    }

    @Override
    public Integer modifyStatus(String pkId, String status, String log) {
        return scriptAnalyzeListMapper.modifyStatus(pkId, status, log);
    }

    @Override
    public Integer modifyStartTsById(String pkId) {
        return scriptAnalyzeListMapper.modifyStartTs(pkId, DateUtil.now());
    }

    @Override
    public Integer modifyEndTsById(String pkId) {
        return scriptAnalyzeListMapper.modifyEndTs(pkId, DateUtil.now());
    }

    @Override
    public ScriptAnalyzeListPo queryById(String pkId) {
        return scriptAnalyzeListMapper.selectById(pkId);
    }

    @Override
    public Integer add(ScriptAnalyzeListPo scriptAnalyzeListPo) {
        return scriptAnalyzeListMapper.insert(scriptAnalyzeListPo);
    }

    @Override
    public Integer modifyById(ScriptAnalyzeListPo scriptAnalyzeListPo) {
        return scriptAnalyzeListMapper.updateById(scriptAnalyzeListPo);
    }

    @Override
    public Integer removeById(String pkId) {
        return scriptAnalyzeListMapper.deleteById(pkId);
    }
}
